<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录 - 天幕</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .login-container {
      background: rgba(255, 255, 255, 0.95);
      padding: 2rem;
      border-radius: 10px;
      box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
      width: 90%;
      max-width: 400px;
    }

    h1 {
      color: #333;
      text-align: center;
      margin-bottom: 2rem;
      font-size: 2rem;
    }

    .form-group {
      margin-bottom: 1.5rem;
    }

    label {
      display: block;
      margin-bottom: 0.5rem;
      color: #555;
      font-size: 0.9rem;
    }

    input {
      width: 100%;
      padding: 0.8rem;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 1rem;
      transition: border-color 0.3s;
    }

    input:focus {
      outline: none;
      border-color: #667eea;
    }

    .captcha-group {
      display: flex;
      gap: 1rem;
      align-items: flex-end;
    }

    .captcha-input {
      flex: 1;
    }

    .captcha-display {
      background: #f0f0f0;
      padding: 0.8rem;
      border-radius: 4px;
      font-family: monospace;
      font-size: 1.2rem;
      color: #333;
      user-select: none;
      cursor: pointer;
      text-align: center;
      min-width: 100px;
    }

    button {
      width: 100%;
      padding: 1rem;
      background: #667eea;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 1rem;
      cursor: pointer;
      transition: background 0.3s;
    }

    button:hover {
      background: #764ba2;
    }

    #refreshCaptcha {
      padding: 0.8rem;
      width: auto;
      background: #f0f0f0;
      color: #333;
    }

    #refreshCaptcha:hover {
      background: #e0e0e0;
    }

    .error-message {
      color: #dc3545;
      font-size: 0.9rem;
      margin-top: 0.5rem;
      display: none;
    }
  </style>
</head>
<body>
  <div class="login-container">
    <h1>天幕系统</h1>
    <form id="loginForm">
      <div class="form-group">
        <label for="username">账号</label>
        <input type="text" id="username" placeholder="请输入账号" required>
        <div class="error-message" id="usernameError"></div>
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" placeholder="请输入密码" required>
        <div class="error-message" id="passwordError"></div>
      </div>
      <div class="form-group">
        <label for="captcha">验证码</label>
        <div class="captcha-group">
          <div class="captcha-input">
            <input type="text" id="captcha" placeholder="请输入验证码" required>
            <div class="error-message" id="captchaError"></div>
          </div>
          <div class="captcha-display" id="captchaText" onclick="getCaptcha()"></div>
        </div>
      </div>
      <button type="submit">登录</button>
    </form>
  </div>

  <script>
    let captchaId = null;

    // 获取验证码
    async function getCaptcha() {
      try {
        const response = await fetch('/captcha');
        const data = await response.json();
        captchaId = data.id;
        document.getElementById('captchaText').textContent = data.text;
      } catch (error) {
        console.error('获取验证码失败:', error);
      }
    }

    // 初始化获取验证码
    getCaptcha();

    // 表单验证
    function validateForm() {
      let isValid = true;
      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const captcha = document.getElementById('captcha').value;

      if (!username) {
        showError('usernameError', '请输入账号');
        isValid = false;
      }

      if (!password) {
        showError('passwordError', '请输入密码');
        isValid = false;
      }

      if (!captcha) {
        showError('captchaError', '请输入验证码');
        isValid = false;
      }

      return isValid;
    }

    function showError(elementId, message) {
      const element = document.getElementById(elementId);
      element.textContent = message;
      element.style.display = 'block';
    }

    function clearErrors() {
      const errors = document.getElementsByClassName('error-message');
      for (let error of errors) {
        error.style.display = 'none';
      }
    }

    // 登录表单提交
    document.getElementById('loginForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      clearErrors();
      
      if (!validateForm()) {
        return;
      }

      const username = document.getElementById('username').value;
      const password = document.getElementById('password').value;
      const captchaText = document.getElementById('captcha').value;

      try {
        const response = await fetch('/login', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            username,
            password,
            captchaId,
            captchaText
          })
        });

        const data = await response.json();
        
        if (data.success) {
          localStorage.setItem('username', data.username);
          window.location.href = '/index';
        } else {
          showError('captchaError', data.error || '登录失败');
          getCaptcha();
        }
      } catch (error) {
        console.error('登录失败:', error);
        showError('captchaError', '登录失败，请重试');
        getCaptcha();
      }
    });
  </script>
</body>
</html>